package com.xbell.app.sys.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.xbell.app.sys.common.PageInfo;
import com.xbell.app.sys.common.ResponseMessage;
import com.xbell.app.sys.service.UserService;

@RestController
@RequestMapping(value="home")
@RefreshScope
public class HomeController  implements ApplicationContextAware{
	
	//@Value("${prod.tech}")
	//String tech;
	
	ApplicationContext ctx = null;
	
	Logger logger = LoggerFactory.getLogger(getClass());
	
	//@Autowired
	HomeBController b;
	
	@Autowired
	public void setB(HomeBController x) {
		this.b = x;	
	}
	
	//@Autowired
	//private LoadBalancerClient loadBalancerClient;
	
	@Autowired
    private RestTemplate restTemplate;
    
    //@Autowired
    //public HomeController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
	
	@Autowired
	private UserService userService;
	
	@GetMapping("/")
	public ResponseMessage<Object> home(){
		//ServiceInstance serviceInstance = loadBalancerClient.choose("appone");
		//String res = restTemplate.getForObject("http://appone/home/?say=" + "hello word", String.class);
		//String path = String.format("%s%s%s",serviceInstance.getHost(),serviceInstance.getPort(),serviceInstance.getUri());
		//System.out.println(JSON.toJSONString(serviceInstance)+path);
		String path = String.format("http://provider/appone/home/?say=%s","say hello");
		return ResponseMessage.ok(restTemplate.getForObject(path,String.class));
	}
	
	@GetMapping("/t2")
	public ResponseMessage<Object> t2(){
		return ResponseMessage.ok(this.userService.delUser("abc", "deluserid===="));
	}
	
	@GetMapping("/allLogs")
	public Object allLogs(PageInfo pg,String title){
		LoggerFactory.getLogger(getClass()).info(ctx.getEnvironment().getProperty("prod.tech"));
		
		return this.userService.allLogs(pg, title);
	}
	
	@PostMapping("/delLog")
	public Object delLog(String ids){
		return this.userService.delLog(ids);
	}
	
	@SentinelResource(value="hello",blockHandler = "helloBlockHandler")
	@GetMapping("/hello")
	public ResponseMessage<Object> hello(String say){
		return ResponseMessage.ok();
	}
	
	public ResponseMessage<Object> helloBlockHandler(String say,BlockException be){
		this.logger.error("降级了");
		return ResponseMessage.error("发生错误来了");
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.ctx = applicationContext;
		this.initRule();
	}
	
	public void initRule() {
		List<FlowRule> rules = new ArrayList<FlowRule>();
		FlowRule rule = new FlowRule("hello");
		rule.setCount(2);
		rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
		rule.setLimitApp("default");
		rules.add(rule);
		FlowRuleManager.loadRules(rules);
	}
	
	

}
